package com.eng.scc.core.parts;

import com.eng.scc.core.data.Vehicle;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;

public class VehiclesTable extends JTable
{
    private static final int COL_COMPANY    = 0;
    private static final int COL_DRIVER     = 1;
    private static final int COL_BRAND_MODEL= 2;
    private static final int COL_PLATE      = 3;
    private static final int COL_CAPACITY   = 4;
    private static final int COL_AVG        = 5;
    private static final int COL_AXLES      = 6;
    private static final int COL_MAX_FUEL   = 7;
    private static final int COL_YEAR_MODEL = 8;
    private static final int COL_TRUCK_TYPE = 9;
    
    private static final String[] COLUMN_NAMES = 
    {
        "Empresa",
        "Motorista",
        "Marca/Modelo",
        "Placa",
        "Cap. Máx. Carga",
        "Média Fabricante",
        "Eixos",
        "Cap. Máx. Combustível",
        "Ano/Modelo",
        "Tipo de Carroceria",
    };

    @Override
    protected JTableHeader createDefaultTableHeader()
    {
        return new JTableHeader( columnModel )
        {
            @Override
            public String getToolTipText( MouseEvent e )
            {
                java.awt.Point p = e.getPoint();
                int index = columnModel.getColumnIndexAtX( p.x );
                int realIndex = columnModel.getColumn( index ).getModelIndex();
                return COLUMN_NAMES[realIndex];
            }
        };
    }
    
    private static class VehicleTableModel extends AbstractTableModel
    {
        private List<Vehicle> vehicles = new ArrayList<Vehicle>();
        
        public VehicleTableModel()
        {
        }

        @Override
        public String getColumnName( int column )
        {
            return COLUMN_NAMES[column];
        }
        
        @Override
        public int getRowCount()
        {
            return vehicles.size();
        }

        @Override
        public int getColumnCount()
        {
            return COLUMN_NAMES.length;
        }

        @Override
        public Object getValueAt( int rowIndex, int columnIndex )
        {
            Vehicle v = vehicles.get( rowIndex );
            
            switch ( columnIndex )
            {
                case COL_AVG:
                    return v.getManufacturingAvg();
                case COL_AXLES:
                    return v.getAxles();
                case COL_BRAND_MODEL:
                    return v.getBrand()+"/"+v.getModel();
                case COL_CAPACITY:
                    return v.getCapacity();
                case COL_COMPANY:
                    return v.getCompanyId();
                case COL_DRIVER:
                    return v.getDriverId();
                case COL_MAX_FUEL:
                    return v.getMaxFuel();
                case COL_PLATE:
                    return v.getPlate();
                case COL_YEAR_MODEL:
                    return v.getModelYear() + "/" + v.getModelYear();
                case COL_TRUCK_TYPE:
                    return v.getTruckModel().getName();
                default:
                    throw new AssertionError();
            }
        }

        @Override
        public boolean isCellEditable( int rowIndex, int columnIndex )
        {
            return false;
        }

        public void setVehicles( List<Vehicle> vehicles )
        {
            this.vehicles = vehicles;
            fireTableDataChanged();
        }

        public List<Vehicle> getVehicles()
        {
            return vehicles;
        }
    }
    
    private VehicleTableModel tableModel = new VehicleTableModel();
    
    public VehiclesTable()
    {
        setModel( tableModel );
//        setRootVisible( false );
//        setShowsRootHandles( true );
        setRowHeight( 25 );
    }
    
    public void setVehicles( List<Vehicle> vehicles )
    {
        tableModel.setVehicles( vehicles );
    }
    
    public List<Vehicle> getVehicles()
    {
        return tableModel.getVehicles();
    }
    
    public Vehicle getSelectedValue()
    {
        if ( getSelectedRow() >= 0 )
        {
             return tableModel.getVehicles().get( getSelectedRow() );
        }
        
        return null;
    }
}